package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class blogdao {
    //1.新增一篇博客
    //2.获取到到所有的博客
    //3.根据id拿到指定博客
    //4.根据id删除博客
    public static void insert(blog blog){
        System.out.println("插入一个博客");
        Connection connection=null;
        PreparedStatement statement=null;
        //1.连接数据库
        try {
            connection=DBUtil.connection();
            //2.构造sql语句
            String sql="insert into blog values(null,?,?,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setTimestamp(3,blog.getPostTime());
            statement.setInt(4,blog.getUseId());
            statement.executeUpdate();
            System.out.println("插入成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    //2.获取到到所有的博客
    public static List<blog> getAllBlog(){
        List<blog>blogs=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            //建立连接
            connection=DBUtil.connection();
            //进行封装SQl语句
            String sql="select * from blog order by postTime desc";
            statement=connection.prepareStatement(sql);
            //执行sql语句
            resultSet=statement.executeQuery();
            while (resultSet.next()) {
                blog blog=new blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content=resultSet.getString("content");
                //博客预览页不需要可以看见整个内容
                if(content.length()>90){
                    content=content.substring(0,90)+"........";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUseId(resultSet.getInt("userId"));
                blogs.add(blog);
            }
            return blogs;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //3.根据id拿到指定博客
    public static blog getBlog(int blogId){
        List<blog>blogs=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            //建立连接
            connection=DBUtil.connection();
            //进行封装SQl语句
            String sql="select * from blog where blogId= ?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //执行sql语句
            resultSet=statement.executeQuery();
            blog blog=new blog();
            //因为是自增主键所以数据不是0就是1
            if (resultSet.next()) {
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUseId(resultSet.getInt("userId"));
            }
            return blog;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //4.根据id删除博客
    public static void delete(int blogId){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //建立连接
            connection=DBUtil.connection();
            //拼装sql语句
            String sql="delete from blog where blogId= ?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //执行sql语句
            int k=statement.executeUpdate();
            if(k==1){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    //根据id进行查有多少篇文章
    public static int count(int Id){
        List<blog>blogs=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            //建立连接
            connection=DBUtil.connection();
            //拼装sql语句
            String sql="select * from blog where blog.userId=?;";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,Id);
            //执行sql语句
            resultSet=statement.executeQuery();
            while (resultSet.next()) {
                blog blog=new blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content=resultSet.getString("content");
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUseId(resultSet.getInt("userId"));
                blogs.add(blog);
            }
            return blogs.size();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return 0;
    }

    public static void main(String[] args) {
//        blog blog=new blog();
//        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
//        blog.setTitle("第5篇博客");
//        blog.setContent("咿呀咿呀哟咿呀咿呀哟咿呀咿呀哟");
//        blog.setUseId(852);
//        blogdao blogdao=new blogdao();
//        blogdao.insert(blog);
//        List<blog>list= blogdao.getAllBlog();
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//        }

//        System.out.println(blogdao.count(852));
    }
}
